package com.zhangmeng.tools.json2Pojo;

import cn.hutool.core.lang.func.LambdaUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JFieldVar;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.Accessors;
import org.jsonschema2pojo.AbstractTypeInfoAwareAnnotator;
import org.jsonschema2pojo.GenerationConfig;

/**
 * @author : 芊芊墨客
 * @version : 1.0
 * @date : 2023-04-21 09:29
 *
 *  * 源代码链接地址: https://blog.csdn.net/weixin_39651356/article/details/127155659
 */
@Setter
@Getter
public class MyAbstractTypeInfoAwareAnnotator extends AbstractTypeInfoAwareAnnotator {

    /**
     * 是否开启使用lombok注解
     */
    boolean lombokAnnotFlag = true;

    /**
     * 是否开启使用swagger注解
     */
    boolean swaggerAnnotFlag = true;

    /**
     * 是否开启使用mybatisplus注解
     */
    boolean mybatisPlusAnnotFlag = true;


    /**
     * 是否开启使用jackson注解
     */
    boolean jacksonAnnotFlag = false;


    public MyAbstractTypeInfoAwareAnnotator(GenerationConfig generationConfig) {
        super(generationConfig);
    }

    @Override
    protected void addJsonTypeInfoAnnotation(JDefinedClass clazz, String propertyName) {
    }

    /**
     * 类上的注解
     */
    @Override
    public void propertyInclusion(JDefinedClass clazz, JsonNode schema) {
        if (lombokAnnotFlag) {
            clazz.annotate(Data.class);
            clazz.annotate(ToString.class).param(LambdaUtil.getMethodName(ToString::callSuper), true);
            clazz.annotate(Builder.class);
            clazz.annotate(NoArgsConstructor.class);
            clazz.annotate(AllArgsConstructor.class);
            clazz.annotate(Accessors.class).param(LambdaUtil.getMethodName(Accessors::chain), true);
        }
        if (mybatisPlusAnnotFlag) {
            clazz.annotate(TableName.class).param(LambdaUtil.getMethodName(TableName::value), StrUtil.toUnderlineCase(clazz.name()));
        }
        if (swaggerAnnotFlag) {
            clazz.annotate(ApiModel.class).param(LambdaUtil.getMethodName(ApiModel::description), clazz.name());
        }

    }


    /**
     * 属性字段上的注解
     */
    @Override
    public void propertyField(JFieldVar field, JDefinedClass clazz, String propertyName, JsonNode propertyNode) {
        if (mybatisPlusAnnotFlag) {
            field.annotate(TableField.class).param("value", StrUtil.toUnderlineCase(propertyName));
        }
        if (swaggerAnnotFlag) {
            field.annotate(ApiModelProperty.class).param("value", propertyName);
        }
        if (jacksonAnnotFlag) {
            field.annotate(JsonProperty.class).param("value", propertyName);
        }
    }
}


